package com.metronoteapp.database;

import com.metronoteapp.model.NoteModel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class NoteDao extends BaseDao {

	public static final String TABLE_NAME = "NoteTbl";
	public static final String ID_NOTE = "IdNote";
	public static final String TITLE_NOTE = "TitleNote";
	public static final String CONTENT_NOTE = "ContentNote";
	public static final String GROUP_ID = "GroupId";
	public static final String FLG_FAV = "flgFav";
	public static final String TYPE = "NoteType";
	public static final String DATE_TIME = "DateTime";
	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
			+ "( " + ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ TITLE_NOTE + " TEXT, " + CONTENT_NOTE + " TEXT, " + TYPE
			+ " TEXT, " + DATE_TIME + " TEXT, " + GROUP_ID + " INTEGER, "
			+ FLG_FAV + " INTEGER)";
	public static final String[] COLUMNS = { ID_NOTE, TITLE_NOTE, CONTENT_NOTE,
			TYPE, DATE_TIME, GROUP_ID, FLG_FAV };

	public NoteDao() {
	}

	public NoteDao(Context _context) {
		super(_context);
	}

	// Insert new record
	public long insertNote(NoteModel _note) {
		ContentValues cv = new ContentValues();
		cv.put(TITLE_NOTE, _note.getTitle());
		cv.put(CONTENT_NOTE, _note.getContent());
		cv.put(GROUP_ID, _note.getGroupId());
		cv.put(FLG_FAV, _note.getFlgFav());
		cv.put(TYPE, _note.getNoteType());
		cv.put(DATE_TIME, _note.getDateTime());
		return mDB.insert(TABLE_NAME, null, cv);
	}

	// Edit one record
	public long updateNote(NoteModel _note) {
		ContentValues values = new ContentValues();
		String selection = ID_NOTE + " = '" + _note.getId() + "'";
		values.put(ID_NOTE, _note.getId());
		values.put(TITLE_NOTE, _note.getTitle());
		values.put(CONTENT_NOTE, _note.getContent());
		values.put(GROUP_ID, _note.getGroupId());
		values.put(FLG_FAV, _note.getFlgFav());
		values.put(TYPE, _note.getNoteType());
		values.put(DATE_TIME, _note.getDateTime());
		return mDB.update(TABLE_NAME, values, selection, null);
	}

	// Remove one record
	public boolean removeNote(NoteModel _note) {
		return mDB.delete(TABLE_NAME, ID_NOTE + "=" + _note.getId(), null) > 0;
	}

	// Select all record
	public Cursor getAllRecord() {
		return mDB.query(TABLE_NAME, new String[] { ID_NOTE, TITLE_NOTE,
				CONTENT_NOTE, TYPE, DATE_TIME, GROUP_ID, FLG_FAV }, null, null,
				null, null, TITLE_NOTE);
	}

	// Select one record
	public Cursor getNote(NoteModel _note) {
		Cursor mCursor = mDB.rawQuery("SELECT * FROM " + TABLE_NAME
				+ " WHERE TRIM(IdNote) = '" + _note.getId() + "'", null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;
	}

	// Select all record imp
	public Cursor getNoteImportant() {
		Cursor mCursor = mDB.rawQuery("SELECT * FROM " + TABLE_NAME
				+ " WHERE TRIM(flgFav) = '1' ORDER BY TitleNote", null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;
	}

	// Select one record
	public Cursor getNoteByGroup(NoteModel _note) {
		Cursor mCursor = mDB.rawQuery("SELECT * FROM " + TABLE_NAME
				+ " WHERE TRIM(GroupId) = '" + _note.getGroupId()
				+ "' ORDER BY TitleNote", null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;
	}
}
